#include "mainform.h"

#define DEVCONFWIDGET 0
#define CHNWIDGET 1
#define M61850WIDGET 2
#define ENDWIDGET 3


extern void clearData();
extern bool parseDev(QString filepath);
extern bool parseDatamap();
extern QList<DevConf> confDevList;
extern FtpUtil ftp;
extern bool loadBoxInfo(const QString &filename);
extern BoxInfo boxInfo;
extern RunState runState;
extern QByteArray pstxt;


MainForm::MainForm()
{
   setupUi(this);


   if(!loadBoxInfo("./config/box.cfg"))
   {
       QMessageBox::critical(this,"警告","加载box.cfg失败,可能会对程序运行产生影响");
   }
   ftp.initConn(boxInfo.dfip,boxInfo.user,boxInfo.psw);
   ftp.connectFtp();


   //warn:以下这段代码顺序不要随便修改
   stackedWidget->removeWidget(page);
   stackedWidget->removeWidget(page_2);
   stackedWidget->addWidget(devWidget=new DevWidget);
   stackedWidget->addWidget(chnWidget=new ChnWidget);
   stackedWidget->addWidget(m61850Widget=new M61850Widget);
   stackedWidget->addWidget(endwidget=new EndWidget);
   stackedWidget->addWidget(updateConfWidget=new UpdateConfWidget);
   stackedWidget->addWidget(box=new BoxWidget);
   stackedWidget->addWidget(ucConfigWidget=new UcConfigWidget);
   //end warn


   stateThread=new StateThread;
   logDlg=new LogDlg;

   this->setFixedSize(787,560);

   viewOneConf();
   createStatusBar();


   connect(iedAction,SIGNAL(triggered()),this,SLOT(viewOneConf()));
   connect(oneConfAction,SIGNAL(triggered()),this,SLOT(startOneConf()));
   connect(devWidget,SIGNAL(mystep(int)),this,SLOT(switchPage(int)));
   connect(chnWidget,SIGNAL(mystep(int)),this,SLOT(switchPage(int)));
   connect(m61850Widget,SIGNAL(mystep(int)),this,SLOT(switchPage(int)));
   connect(endwidget,SIGNAL(mystep(int)),this,SLOT(switchPage(int)));
   connect(boxSetAction,SIGNAL(triggered()),this,SLOT(viewBoxSet()));
   connect(logAction,SIGNAL(triggered()),this,SLOT(viewLog()));
   connect(&ftp,SIGNAL(updateStatus(LogType,LogLevel,QString)),this,SLOT(updateStatusBar(LogType,LogLevel,QString)));
   connect(this,SIGNAL(addLog(LogType,LogLevel,QString)),logDlg,SLOT(addLog(LogType,LogLevel,QString)));
   connect(box,SIGNAL(openLogDlg()),this,SLOT(viewLog()));
   connect(box,SIGNAL(operlog(QString)),logDlg,SLOT(Oper(QString)));
   connect(&ftp,SIGNAL(updateRunstate()),this,SLOT(updateRunState()));
   connect(this->helpAction,SIGNAL(triggered()),this,SLOT(openHelp()));
   connect(ucconfigAction,SIGNAL(triggered()),this,SLOT(openUcConfig()));

   stateThread->start();
}

MainForm::~MainForm()
{
    clearData();
    if(!stackedWidget)
        delete stackedWidget;
}

void MainForm::startOneConf()
{
//    if(confDevList.isEmpty())
//    {
        parseDev("./config/dev.cfg");
        parseDatamap();
        devWidget->mDevModel->setDatas(confDevList);
//    }
    stackedWidget->setCurrentIndex(0);
}


void MainForm::switchPage(int i)
{
    if(i==1)
    {
        this->stackedWidget->setCurrentIndex(stackedWidget->currentIndex()+1);
    }
    else if(i==-1)
    {
        this->stackedWidget->setCurrentIndex(stackedWidget->currentIndex()-1);
    }

    if(stackedWidget->currentIndex()==CHNWIDGET)
    {
        chnWidget->refreshData();
    }
    else if(stackedWidget->currentIndex()==ENDWIDGET)
    {
        endwidget->refreshData();
    }
}


void MainForm::viewOneConf()
{
   // clearData();
    stackedWidget->setCurrentIndex(4);
    updateConfWidget->initUI();
}


void MainForm::viewBoxSet()
{
    stackedWidget->setCurrentIndex(5);
}


void MainForm::createStatusBar()
{
    statusLabel=new QLabel;
    //statusLabel->setAlignment(Qt::AlignHCenter);
    statusBar()->insertWidget(0, statusLabel, 1);
    statusBar()->setSizeGripEnabled(FALSE);
    //statusBar()->addWidget();

    //connect(box,SIGNAL(updateStatus(QString)),this,SLOT(updateStatusBar(QString)));
}

void MainForm::updateStatusBar(LogType type,LogLevel level,const QString &msg)
{
    //qDebug("%s",qPrintable(msg));

   emit addLog(type,level,msg);
}


void MainForm::viewLog()
{
    logDlg->setModal(false);
    logDlg->show();
}

void MainForm::updateRunState()
{
    runState.scl_srvr_n=false;
    runState.watchdog=false;
    runState.integratedpower=false;


    QString line(pstxt);
    if(line.contains("watchdog"))
    {
        runState.watchdog=true;

    }
    if(line.contains("scl_srvr_n"))
    {
        runState.scl_srvr_n=true;

    }
    if(line.contains("integratedpower"))
    {
        runState.integratedpower=true;
    }



    QString text;
    if(runState.watchdog)
    {
        text.append("友情提示:盒子的程序正在运行,上传文件之前最好取消自启动,重启盒子之后再上传");
    }
    else
    {
        text.append("程序正在没运行已停止").append("\t");
    }

//    if(runState.integratedpower)
//    {
//        text.append("界面程序:运行中").append("\t");
//    }
//    else
//    {
//        text.append("界面程序:停止").append("\t");
//    }
//    if(runState.scl_srvr_n)
//    {
//        text.append("61850:运行中").append("\t");
//    }
//    else
//    {
//        text.append("61850:停止").append("\t");
//    }
    statusLabel->setText(text);
}

 void MainForm::openHelp()
 {
         QDesktopServices::openUrl ( QUrl::fromLocalFile("./help.pdf") );
 }

 void MainForm::openUcConfig()
 {
     stackedWidget->setCurrentWidget(ucConfigWidget);
 }
